<template>
  <div class="container">
    <div class="header">
      <van-nav-bar title="附近农庄" left-text="返回" right-text left-arrow @click-left="onClickLeft"/>
    </div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <div class="serach_box">
        <div class="left">
          <img src="../assets/images/adress.png" alt>
          <span>{{positionText}}</span>
        </div>
        <div class="center">
          <van-search
            placeholder="搜索"
            v-model="searchValue"
            @search="searchMethod"
            @clear="clearSearch"
          />
        </div>
        <div class="right" @click="toMyAdopt">
          <img src="../assets/images/user.png" alt>
          <span>我的认养</span>
          <div class="mark"></div>
        </div>
      </div>
      <div class="banner">
        <img src="../assets/images/banner.png" alt>
      </div>
      <div class="nav">
        <div class="item mark" @click="allSelect">全部</div>
        <div class="item" @click="projectToggle">
          <span>娱乐项目</span>
          <img src="../assets/images/arrow.png" alt>
        </div>
        <div class="item" @click="locationSearchToggle">
          <span>位置搜索</span>
          <img src="../assets/images/arrow.png" alt>
        </div>
        <div class="item" @click="sortingToggle">
          <span>智能排序</span>
          <img src="../assets/images/arrow.png" alt>
        </div>
      </div>
      <div class="filterPop" v-if="isShowFilterPop">
        <div class="content">
          <div class="popList">
            <div class="project" v-show="isShowProject">
              <ul>
                <li v-for="(item,index) in projectList" :key="index">
                  <span
                    :class="{selectProject:item.isSelect}"
                    @click="selectProjectMethod(item)"
                  >{{item.name}}</span>
                </li>
              </ul>
            </div>
            <div class="locationSearch" v-show="isShowLocationSearch">
              <div
                v-for="(item,index) in locationList"
                :key="index"
                :class="{selectColor:item.selectColor}"
                @click="selectPrivinceMethod(item)"
              >{{item.name}}</div>
            </div>
            <div class="sorting" v-show="isShowSorting">
              <div
                v-for="(item,index) in sortList"
                :key="index"
                :class="{selectColor:item.selectColor}"
                @click="selectItem(item)"
              >{{item.name}}</div>
            </div>
          </div>
          <div class="sure" @click="sureFilter">确定</div>
        </div>
      </div>

      <div class="tab">
        <div class="list">
          <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
            <ul>
              <li
                v-for="(item,index) in initData.list"
                :key="index"
                @click="toFarmIndex(item.shopId)"
              >
                <div class="box_content">
                  <div class="left">
                    <img :src="item.logo_url" alt>
                  </div>
                  <div class="right">
                    <div class="title">{{item.shopName}}</div>
                    <div class="rate">
                      <van-rate
                        v-model="item.rating"
                        color="#01bf47"
                        :size="22"
                        readonly
                        void-color="#cccccc"
                      />
                    </div>
                    <div class="subtitle">{{item.description}}</div>
                    <div class="classification">
                      <div v-for="(at,eq) in item.keyvalueList" :key="eq">{{at}}</div>
                    </div>
                    <div class="adress">
                      <img src="../assets/images/adress3.png" alt>
                      <span>{{item.address}}</span>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </van-list>
        </div>
      </div>
    </van-pull-refresh>
    <positionMap ref="pMap"></positionMap>
  </div>
</template>

<script>
import $province from "../common/province";
import positionMap from "../components/positionMap";
export default {
  name: "nearFarm",
  components: {
    positionMap
  },
  data() {
    return {
      //定位省市区
      positionText: "",
      swipeNum: 6,
      lineHeight: 0,
      active: 0,
      tabList: [],
      //搜索关键字
      searchValue: "",
      value: 4,
      initData: {},
      initDataList: [],
      typeId: "",
      //下拉刷新
      isLoading: false,
      //上拉加载
      loading: false,
      finished: false,
      pageNum: 1,
      //是否显示过滤弹窗
      isShowFilterPop: false,
      //是否显示娱乐项目弹窗内容
      isShowProject: false,
      //是否显示位置搜索弹窗内容
      isShowLocationSearch: false,
      //是否显示智能排序弹窗内容
      isShowSorting: true,
      //智能排序列表
      sortList: [
        {
          name: "离我最近",
          selectColor: false
        },
        {
          name: "好评优先",
          selectColor: false
        },
        {
          name: "最近发布",
          selectColor: false
        }
      ],
      //娱乐项目列表
      projectList: [],
      //位置搜索列表
      locationList: [],
      //选中的娱乐项目
      types: [],
      //选中的位置
      selectPrivince: "",
      //选中的智能排序类型
      sortType: "",
      //定位省份名字
      locationProvince: "",
      //定位城市名字
      locationCity: "",
      //定位区县名字
      locationDistrict: "",
      //定位省份编码
      locationProvince_code: "",
      //定位城市编码
      locationCity_code: "",
      //定位区县编码
      locationDistrict_code: "",
      positionAddress: {},
      province_code: "",
      city_code: "",
      district_code: ""
    };
  },
  watch: {
    isShowFilterPop() {
      if (!this.isShowFilterPop) {
        this.projectList.forEach((item, index) => {
          item.isSelect = false;
        });
        this.locationList.forEach((item, index) => {
          item.selectColor = false;
        });
        this.sortList.forEach((item, index) => {
          item.selectColor = false;
        });
      }
    },
    //获取定位地址
    positionAddress() {
      if (this.positionAddress && this.positionAddress.addressComponent) {
        this.positionText = this.positionAddress.addressComponent.province;
        this.locationProvince = this.positionAddress.addressComponent.province;
        this.locationCity = this.positionAddress.addressComponent.city;
        this.locationDistrict = this.positionAddress.addressComponent.district;
        this.getAddressCode(this.locationProvince,'province');
        this.getAddressCode(this.locationCity,'city');
        this.getAddressCode(this.locationDistrict,'district');
      }
    }
  },
  mounted() {
    this.getTabList();
    this.getInitData();
    this.getLocationList();
  },
  methods: {
    //返回
    onClickLeft() {
      this.$toast("返回");
    },
    //搜索
    searchMethod() {
      this.getInitData();
    },
    //清除搜索
    clearSearch() {
      this.getInitData();
    },
    //跳转农场主页
    toFarmIndex(shopId) {
      this.$router.push({
        path: "./farmIndex",
        query: {
          shopId: shopId
        }
      });
    },
    //tab切换
    tabChange(index, title) {
      if (this.tabList[index].id) {
        this.typeId = this.tabList[index].id;
      } else {
        this.typeId = "";
      }
      this.initDataList = [];
      this.pageNum = 1;
      this.finished = false;
      this.getInitData();
    },
    //下拉刷新
    onRefresh() {
      this.initDataList = [];
      this.pageNum = 1;
      this.finished = false;
      this.getTabList();
      this.getInitData();
    },
    //上拉加载
    onLoad() {
      if (this.initData.list && this.initData.list.length > 0) {
        this.getInitData();
      }
    },
    //获取tab列表
    async getTabList() {
      let params = {
        params: {
          parentId: 207
          // isAll: 1
        }
      };
      const res = await this.$axios.get(this.$api.list, params);
      if (res.success) {
        this.projectList = res.data;
        this.projectList.forEach((item, index) => {
          item.isSelect = false;
        });
        console.log(this.projectList);
        this.isLoading = false;
        this.loading = false;
      } else {
        this.tabList = [];
        this.$toast(res.msg);
      }
    },
    //获取列表数据
    async getInitData() {
      let params = {
        pageNum: this.pageNum,
        pageSize: 10,
        shopName: this.searchValue || "",
        province_code: this.province_code,
        city_code: this.city_code,
        district_code: this.district_code,
        province: this.selectPrivince,
        type: this.sortType,
        type_id: this.types
      };
      const res = await this.$axios.post(this.$api.nearbyFarmList, params);
      if (res.success) {
        if (this.loading) {
          this.initDataList = this.initDataList.concat(res.data.list);
          this.initData = res.data;
          this.initData.list = this.initDataList;
          if (this.initData.list.length >= this.initData.total) {
            this.finished = true;
          } else {
            this.pageNum++;
          }
        } else {
          this.initData = res.data;
          if (this.initData.list.length >= this.initData.total) {
            this.finished = true;
          }
        }
        this.initData.list.forEach((item, index) => {
          item.keyvalueList = item.key_value.split(",");
        });

        this.isLoading = false;
        this.loading = false;
      } else {
        this.initData = {};
        this.$toast(res.msg);
      }
    },
    //获取位置搜索信息
    getLocationList() {
      this.locationList = $province.province;
      this.locationList.forEach((item, index) => {
        item.selectColor = false;
      });
      // console.log(this.locationList);
    },
    //筛选切换
    allSelect() {
      this.locationProvince_code = "";
      this.locationCity_code = "";
      this.locationDistrict_code = "";
      this.selectPrivince = "";
      this.sortType = "";
      this.types = [];
      this.isShowFilterPop = false;
      this.getInitData();
    },
    projectToggle() {
      if (this.isShowFilterPop && this.isShowProject) {
        this.isShowFilterPop = false;
      } else {
        this.isShowFilterPop = true;
        this.isShowProject = true;
        this.isShowLocationSearch = false;
        this.isShowSorting = false;
      }
    },
    locationSearchToggle() {
      if (this.isShowFilterPop && this.isShowLocationSearch) {
        this.isShowFilterPop = false;
      } else {
        this.isShowFilterPop = true;
        this.isShowLocationSearch = true;
        this.isShowProject = false;
        this.isShowSorting = false;
      }
    },
    sortingToggle() {
      if (this.isShowFilterPop && this.isShowSorting) {
        this.isShowFilterPop = false;
      } else {
        this.isShowFilterPop = true;
        this.isShowSorting = true;
        this.isShowProject = false;
        this.isShowLocationSearch = false;
      }
    },
    //智能排序选择
    selectItem(item) {
      this.sortList.forEach((item, index) => {
        item.selectColor = false;
      });
      item.selectColor = true;
    },
    //娱乐项目选择
    selectProjectMethod(item) {
      item.isSelect = !item.isSelect;
      this.$forceUpdate();
    },
    //省份选择
    selectPrivinceMethod(item) {
      this.locationList.forEach((item, index) => {
        item.selectColor = false;
      });
      item.selectColor = true;
      this.$forceUpdate();
    },
    //确定筛选
    sureFilter() {
      this.types = [];
      this.projectList.forEach((item, index) => {
        if (item.isSelect) {
          this.types.push(item.id);
        }
      });
      this.locationList.forEach((item, index) => {
        if (item.selectColor) {
          this.selectPrivince = item.name;
        }
      });
      this.sortList.forEach((item, index) => {
        if (item.selectColor && item.name == "好评优先") {
          this.sortType = "rating";
        } else if (item.selectColor && item.name == "最近发布") {
          this.sortType = "update";
        } else if (item.selectColor && item.name == "离我最近") {
          this.province_code = this.locationProvince_code;
          this.city_code = this.locationCity_code;
          this.district_code = this.locationDistrict_code;
        }
      });
      this.getInitData();
      this.isShowFilterPop = false;
    },
    toMyAdopt() {
      this.$router.push({
        path: "/myAdopt",
        query: {}
      });
    },
    async getAddressCode(addressName,type){
       let params = {
        addressName: addressName,
      };
      const res = await this.$axios.post(this.$api.getAddressCode, params);
      if(res.success){
          if(type=='province'){
            this.locationProvince_code=res.data;
          }else if(type=='city'){
            this.locationCity_code=res.data;
          }else if(type=='district'){
            this.locationDistrict_code=res.data;
          };
      }else{

      };
    }
  }
};
</script>

<style scoped>
.selectColor {
  color: #00be46 !important;
}
.selectProject {
  color: #fff !important;
  background: #00be46 !important;
}
.nav {
  width: 100%;
  height: 0.4rem;
  background: #fff;
  display: flex;
  display: -webkit-flex;
  justify-content: space-around;
  align-items: center;
  position: relative;
}
.filterPop {
  position: absolute;
  top: 1.87rem;
  left: 0;
  width: 100%;
  height: calc(100vh - 2.55rem);
  background: rgba(0, 0, 0, 0.6);
  border-top: 1px solid #eeeeee;
  z-index: 100;
}
.filterPop .content {
  background: #fff;
  height: 1.87rem;
  padding-bottom: 0.45rem;
  position: relative;
}
.filterPop .content .popList {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.filterPop .content .sure {
  position: absolute;
  background: #00be46;
  color: #fff;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 0.45rem;
  display: flex;
  display: -webkit-flex;
  font-size: 0.16rem;
}
.filterPop .project ul {
  width: 100%;
  display: flex;
  display: -webkit-flex;
  flex-wrap: wrap;
  padding-top: 0.08rem;
}
.filterPop .project ul li {
  width: 33.33%;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  padding: 0.08rem 0;
}
.filterPop .project ul li span {
  display: flex;
  display: -webkit-flex;
  background: #f5f5f5;
  width: 0.8rem;
  height: 0.3rem;
  justify-content: center;
  align-items: center;
  color: #808080;
  font-size: 0.14rem;
  border-radius: 0.03rem;
  -webkit-border-radius: 0.03rem;
}
.filterPop .sorting,
.filterPop .locationSearch {
  width: 100%;
}
.filterPop .sorting > div,
.filterPop .locationSearch > div {
  width: 100%;
  height: 0.4rem;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  color: #404040;
  font-size: 0.15rem;
}
.nav .item {
  display: flex;
  display: -webkit-flex;
  align-items: center;
  font-size: 0.15rem;
  color: #333333;
}
.nav .mark {
  color: #01bf47;
}
.nav .item img {
  width: 0.1rem;
}
.nav .item span {
  padding-right: 0.06rem;
}
.container {
  padding-top: 0.46rem;
  width: 100%;
}
.header {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
}
.serach_box {
  width: 100%;
  height: 0.3rem;
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.22rem;
  padding: 0 0.16rem;
}
.serach_box .left {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
  width: 20%;
}
.serach_box .left img {
  width: 0.13rem;
}
.serach_box .left span {
  font-size: 0.14rem;
  color: #000000;
  padding-left: 0.06rem;
}
.serach_box .right {
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  width: 25%;
  height: 100%;
  background: #fff;
  box-shadow: 0px 3px 8px 0px rgba(107, 107, 107, 0.19);
  border-radius: 5px;
  -webkit-border-radius: 5px;
  position: relative;
}
.serach_box .right img {
  width: 0.12rem;
}
.serach_box .right span {
  font-size: 0.12rem;
  color: #00bf46;
  padding-left: 0.04rem;
}
.serach_box .right .mark {
  width: 0.09rem;
  height: 0.09rem;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  background: #f44736;
  position: absolute;
  top: -0.04rem;
  right: -0.04rem;
}
.serach_box .center {
  width: 55%;
  height: 100%;
}
.van-search {
  padding: 0 0.14rem;
}
.banner {
  width: 100%;
  margin-top: 0.15rem;
  padding: 0 0.08rem;
}
.banner img {
  width: 100%;
}
.list {
  width: 100%;
  height: 100%;
  padding: 0 0.16rem;
  min-height: calc(100vh - 2.55rem);
}
.list li {
  width: 100%;
  background: #fff;
  border-radius: 0.1rem;
  -webkit-border-radius: 0.1rem;
  margin-top: 0.08rem;
  padding: 0 0.15rem;
}
.list .box_content {
  width: 100%;
  /* height: 1.56rem; */
  padding: 0.15rem 0;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
  border-bottom: 1px solid #f7f7f7;
}
.list .box_content .left {
  min-height: 1.05rem;
  display: flex;
  -webkit-display: flex;
  justify-content: center;
  align-items: center;
}
.list .box_content .left img {
  width: 1.05rem;
  border-radius: 0.05rem;
  -webkit-border-radius: 0.05rem;
}
.list .box_content .right {
  padding-left: 0.15rem;
}
.list .box_content .right .title {
  font-size: 0.15rem;
  color: #333333;
  margin-bottom: 0.06rem;
}
.list .box_content .right .subtitle {
  font-size: 0.12rem;
  color: #8c8c8c;
  margin-bottom: 0.06rem;
}
.list .box_content .right .classification {
  display: flex;
  -webkit-display: flex;
}
.list .box_content .right .classification > div {
  width: 0.45rem;
  height: 0.2rem;
  background: #e4ffeb;
  color: #04b746;
  border-radius: 3px;
  display: flex;
  -webkit-display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 0.1rem;
  line-height: 0;
}
.list .box_content .right .adress {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
  margin-top: 0.06rem;
}
.list .box_content .right .adress img {
  width: 0.13rem;
}
.list .box_content .right .adress span {
  font-size: 0.14rem;
  color: #808080;
  padding-left: 0.06rem;
}
.box_footer {
  width: 100%;
  height: 0.42rem;
  display: flex;
  -webkit-display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.12rem;
  color: #333333;
}
.box_footer span {
  color: #f44736;
  font-size: 0.16rem;
}
</style>


<style>
.van-tab--active {
  color: #01bf47;
}
.van-search .van-cell {
  border-radius: 50px;
  background: #e8e8e8;
}
</style>
